﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.SXXK
{
	/// <summary>
	/// Sản phẩm xuất khẩu.
	/// </summary>
	public class SanPham
	{
		public static DataSet GetDanhSach(string maHaiQuan, string maDoanhNghiep)
		{
			string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
			string query = "SELECT SP.Ma_HQ AS MaHaiQuan, SP.Ma_SP AS Ma, SP.Ten_SP AS Ten, SP.Ma_HS AS MaHS, SP.MA_DVT AS DVT_ID FROM SSP SP WHERE SP.MA_HQ = @MA_HQ AND MA_DV = @MA_DV";

			SqlParameter[] parameters = new SqlParameter[2];

			parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
			parameters[0].Size = 6;
			parameters[0].Direction = ParameterDirection.Input;
			parameters[0].Value = maHaiQuan;

			parameters[1] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
			parameters[1].Size = 14;
			parameters[1].Direction = ParameterDirection.Input;
			parameters[1].Value = maDoanhNghiep;

			return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);
		}

        public static bool CheckExist(SqlTransaction transaction, string maHaiQuan, string maDoanhNghiep, string maSP)
        {
            string query = "SELECT COUNT(*) FROM SSP WHERE (MA_HQ = @MaHaiQuan) AND (MA_DV = @MaDoanhNghiep) AND (MA_SP = @MaSP)";
            SqlParameter[] parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@MaHaiQuan", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@MaDoanhNghiep", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maDoanhNghiep;

            parameters[2] = new SqlParameter("@MaSP", SqlDbType.VarChar);
            parameters[2].Size = 30;
            parameters[2].Direction = ParameterDirection.Input;
            parameters[2].Value = maSP;

            object obj = SqlHelper.ExecuteScalar(transaction, CommandType.Text, query, parameters);
            if (obj != null)
            {
                if (Convert.ToInt32(obj) > 0)
                    return true;
            }
            return false;
        }

        public static string[] CheckExist(string maHaiQuan, string maDoanhNghiep, string[] danhSachMaSP)
        {
            ArrayList temp = new ArrayList();
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    foreach (string maSP in danhSachMaSP)
                    {
                        if (CheckExist(trans, maHaiQuan, maDoanhNghiep, maSP))
                        {
                            temp.Add(maSP);
                        }
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
            return (string[])temp.ToArray(typeof(string));
        }
	}
}